import Vue from 'vue';
import Vuex from 'vuex';
import localStorageApi from '@/api/localStorageApi';
import clone from '@/lib/clone';
import createId from '@/lib/createId';
import router from '@/router';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    recordList: [] as RecordItem[],
    localStorageRecordKeyName: 'recordList',
    tagList: [] as Tag[],
    localStorageTagKeyName: 'tagList',
    currentTag: {} as Tag
  },
  getters: {},
  mutations: {
    initRecords(state, records) {
      state.recordList = records;
    },
    createRecord(state, record: RecordItem) {
      const record2: RecordItem = clone(record);
      record2.createdAt = record.createdAt || new Date();
      state.recordList.push(record2);
    },
    initTags(state, tags) {
      state.tagList = tags;
    },
    createTag(state, name: string) {
      const id = createId().toString();
      const names = state.tagList.map(item => item.name);
      if (names.indexOf(name) >= 0) {
        alert('标签名重复');
      } else {
        state.tagList.push({id, name: name});
      }
    },
    updateTag(state, {id, name}) {
      const tag = state.tagList.filter(item => item.id === id)[0];
      if (tag) {
        const tagName = state.tagList.filter(item => item.name === name)[0];
        if (!tagName) {
          tag.name = name;
        } else {
          alert('标签名重复');
        }
      }
    },
    removeTag(state, id: string) {
      const index = state.tagList.map(item => item.id).indexOf(id);
      if (index >= 0) {
        state.tagList.splice(index, 1);
      }
    },
    findTag(state, id: string) {
      state.currentTag = state.tagList.filter(item => item.id === id)[0];
    }
  },
  actions: {
    getRecordList({commit, state}) {
      localStorageApi.getStore(state.localStorageRecordKeyName, (records: any) => {
        commit('initRecords', records);
      });
    },
    saveRecords({commit, state}, record: RecordItem) {
      commit('createRecord', record);
      localStorageApi.setStore(state.localStorageRecordKeyName, JSON.stringify(state.recordList));
    },
    getTagList({commit, state}) {
      localStorageApi.getStore(state.localStorageTagKeyName, (tags: any) => {
        if (tags.length === 0) {
          tags = [
            {id: createId().toString(), name: '衣'},
            {id: createId().toString(), name: '食'},
            {id: createId().toString(), name: '住'},
            {id: createId().toString(), name: '行'},
          ];
          localStorageApi.setStore(state.localStorageTagKeyName, JSON.stringify(tags));
        }
        commit('initTags', tags);
      });
    },
    saveTags({commit, state}, {id, name, isAdd}) {
      isAdd ? commit('createTag', name) : commit('updateTag', {id, name});
      localStorageApi.setStore(state.localStorageTagKeyName, JSON.stringify(state.tagList));
    },
    delTag({commit, state}, id) {
      commit('removeTag', id);
      localStorageApi.setStore(state.localStorageTagKeyName, JSON.stringify(state.tagList));
      router.back();
    },
  },
  // modules: {}
});

export default store;
